package com.pro.control;/*
    ClassName:JurisdictionController
    Package:com.pro.control
    Description:
    @Date:2021/11/25 14:16
    @Author: 赵自强
*/

import com.pro.entity.*;
import com.pro.service.JurisdictionService;
import com.pro.utils.DateUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/jurisdiction")
public class JurisdictionController {

    @Autowired
    private JurisdictionService jurisdictionService;

    //获取部门ztree树 赵自强 21/11/25
    @RequestMapping("/getZtree")
    public JsonData getZtree(){
        List<JsDept> ztree = jurisdictionService.getZtree();
        List<JsDept> newZtree = new ArrayList<>();

        for (int i = 0; i<ztree.size();i++ ){
            if(ztree.get(i).getParentId() == 0){
                newZtree.add(ztree.get(i));
            }
            for (int k = 0; k<ztree.size();k++ ){
                if(ztree.get(i).getId() == ztree.get(k).getParentId()){
                    ztree.get(i).getChildren().add(ztree.get(k));
                }
            }
        }

        JsonData jsonData = new JsonData();
        return newZtree!=null?jsonData.buildSuccess(newZtree):jsonData.buildError("后台数据错误");
    }

    //根据部门查询后台人员  赵自强 21/11/25
    @RequestMapping("/getBackstageStaffs")
    public JsonData getBackstageStaffs(Integer deptId){
        List<JsUser> backstageStaffs = jurisdictionService.getBackstageStaffs(deptId);
        JsonData jsonData = new JsonData();
        return backstageStaffs!=null?jsonData.buildSuccess(backstageStaffs):jsonData.buildError("后台数据错误");
    }


    //根据条件查询用户 赵自强 21/11/25
    @RequestMapping("/retrieval")
    public JsonData retrieval(JsUser jsUser){
        List<JsUser> retrieval = jurisdictionService.retrieval(jsUser);
        JsonData jsonData = new JsonData();
        return retrieval!=null?jsonData.buildSuccess(retrieval):jsonData.buildError("后台数据错误");
    };

    //后台人员账户的启用 赵自强 21/11/28  enableAccount
    @RequestMapping("/enableAccount")
    public JsonData enableAccount(Integer enabled,Integer userId){
        Boolean aBoolean = jurisdictionService.enableAccount(enabled, userId);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };

    //后台人员账户高级检索 赵自强 21/11/28  advancedRetrieval
    @RequestMapping("/advancedRetrieval")
    public JsonData advancedRetrieval(JsUser jsUser){
        List<JsUser> jsUsers = jurisdictionService.advancedRetrieval(jsUser);
        JsonData jsonData = new JsonData();
        return jsUsers!=null?jsonData.buildSuccess(jsUsers):jsonData.buildError("后台数据错误");
    };

    //部门分类树节点添加 赵自强 21/11/28  addNode
    @RequestMapping("/addNode")
    public JsonData addNode(JsDept jsDept){
        Boolean aBoolean = jurisdictionService.addNode(jsDept);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };


    //部门分类树节点删除 赵自强 21/11/28  delNode
    @RequestMapping("/delNode")
    public JsonData delNode(Integer id){
        Boolean aBoolean = jurisdictionService.delNode(id);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };

    //部门分类树节点编辑 赵自强 21/11/28      JsDept jsDept
    @RequestMapping("/editNode")
    public JsonData editNode(JsDept jsDept){
        Boolean aBoolean = jurisdictionService.editNode(jsDept);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };

    //后台人员 使用中/已封禁 开关  赵自强 21/11/29  kgStatus JsUser jsDept
    @RequestMapping("/kgStatus")
    public JsonData kgStatus(JsUser jsUser){
        Boolean aBoolean = jurisdictionService.kgStatus(jsUser);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };

    //根据id 查询后台用户信息 赵自强 21/11/29 getUserById Integer userId
    @RequestMapping("/getUserById")
    public JsonData getUserById(Integer userId){
        JsUser userById = jurisdictionService.getUserById(userId);
        JsonData jsonData = new JsonData();
        return userById!=null?jsonData.buildSuccess(userById):jsonData.buildError("后台数据错误");
    };

    //根据id 修改后台用户信息 赵自强 21/11/29 editUserById JsUser jsUser
    @RequestMapping("/editUserById")
    public JsonData editUserById(JsUser jsUser){
        Boolean aBoolean = jurisdictionService.editUserById(jsUser);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };

    //根据id 修改后台用户信息 赵自强 21/11/30 getAuthority Integer userId
    @RequestMapping("/getAuthority")
    public JsonData getAuthority(Integer userId){
        List<JsUser> authority = jurisdictionService.getAuthority(userId);
        JsonData jsonData = new JsonData();
        return authority!=null?jsonData.buildSuccess(authority):jsonData.buildError("后台数据错误");
    };

    //editAuthority
    //根据id 修改后台用户信息 赵自强 21/11/30 editAuthority Integer userId
    @RequestMapping("/editAuthority")
    public JsonData editAuthority(Integer userId,Integer authorityId){
        Boolean aBoolean = jurisdictionService.editAuthority(userId, authorityId);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };

    //delAuthority
    //根据id 修改后台用户信息 赵自强 21/11/30 delAuthority
    @RequestMapping("/delAuthority")
    public JsonData delAuthority(Integer userId){
        Boolean aBoolean = jurisdictionService.delAuthority(userId);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };


    //shutDown
    //添加封停记录  赵自强 21/12/1   Closure closure
    @RequestMapping("/shutDown")
    public JsonData shutDown(Closure closure){
        Boolean aBoolean = jurisdictionService.shutDown(closure);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };

    //关闭 是否启用 与 状态按钮  赵自强  21/12/1      statusANDenabled
    @RequestMapping("/statusANDenabled")
    public JsonData statusANDenabled(Integer userId){
        Boolean aBoolean = jurisdictionService.statusANDenabled(userId);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };

    //delShutDown
    //解封  删除封停记录  delShutDown   Integer userId
    @RequestMapping("/delShutDown")
    public JsonData delShutDown(Integer userId){
        Boolean aBoolean = jurisdictionService.delShutDown(userId);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };

    //addUser
    //添加后台用户  addUser JsUser jsUser
    @RequestMapping("/addUser")
    public JsonData addUser(JsUser jsUser){
        Boolean aBoolean = jurisdictionService.addUser(jsUser);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };


    //retrievalDept
    //普通检索部门  赵自强 21/12/6 retrievalDept JsDept jsDept
    @RequestMapping("/retrievalDept")
    public JsonData retrievalDept(JsDept jsDept){
        List<JsDept> jsDepts = jurisdictionService.retrievalDept(jsDept);
        JsonData jsonData = new JsonData();
        return jsDepts!=null?jsonData.buildSuccess(jsDepts):jsonData.buildError("后台数据错误");
    };


    //addDept  editDept
    //部门管理 添加部门 赵自强 21/12/06
    @RequestMapping("/addDept")
    public JsonData addDept(JsDept jsDept){
        Boolean aBoolean = jurisdictionService.addDept(jsDept);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };


    //部门管理 修改部门 赵自强 21/12/06
    @RequestMapping("/editDept")
    public JsonData editDept(JsDept jsDept){
        Boolean aBoolean = jurisdictionService.editDept(jsDept);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };


    //部门管理 删除部门 赵自强 21/11/06 delDept Integer id
    @RequestMapping("/delDept")
    public JsonData delDept(Integer id){
        Boolean aBoolean = jurisdictionService.delDept(id);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };


    //部门管理  状态开关  赵自强 21/12/7  statusSwitch Integer id,Integer status
    @RequestMapping("/statusSwitch")
    public JsonData statusSwitch(Integer id,Integer status){
        Boolean aBoolean = jurisdictionService.statusSwitch(id, status);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };

    //getUnsealList 后台人员解封日志 赵自强 21/12/7
    @RequestMapping("/getUnsealList")
    public JsonData getUnsealList(){
        List<Unseal> unsealList = jurisdictionService.getUnsealList();
        JsonData jsonData = new JsonData();
        return unsealList!=null?jsonData.buildSuccess(unsealList):jsonData.buildError("后台数据错误");
    };

    //getShutDown 获取封禁信息  赵自强 21/12/7
    @RequestMapping("/getShutDown")
    public JsonData getShutDown(Integer userId){
        Closure shutDown = jurisdictionService.getShutDown(userId);
        JsonData jsonData = new JsonData();
        return shutDown!=null?jsonData.buildSuccess(shutDown):jsonData.buildError("后台数据错误");
    };


    //addUnseal  添加解封日志  赵自强 21/12/7
    @RequestMapping("/addUnseal")
    public JsonData addUnseal(Unseal unseal){
        Boolean aBoolean = jurisdictionService.addUnseal(unseal);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("后台数据错误");
    };

    //解封日志检索  赵自强 21/12/7 unsealRetrieval Unseal unseal
    @RequestMapping("/unsealRetrieval")
    public JsonData unsealRetrieval(Unseal unseal){
        List<Unseal> unseals = jurisdictionService.unsealRetrieval(unseal);
        JsonData jsonData = new JsonData();
        return unseals!=null?jsonData.buildSuccess(unseals):jsonData.buildError("后台数据错误");
    };


    //导入后台人员  赵自强  21/12/14
    @RequestMapping(value = "/importExcel")
    @ResponseBody
    public JsonData importExcel(MultipartFile file){
        JsUser jsUser = new JsUser();
        String originalFilename = file.getOriginalFilename();

        InputStream in = null;
        try {
            //将file转InputStream
            in = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*if (originalFilename.substring(originalFilename.indexOf(".")) == "xlsx"){
            //将InputStream转XLS对象（即表格对象）
            XSSFWorkbook xssfWorkbook = null;
        }
        if (originalFilename.substring(originalFilename.indexOf("." )) == "xls"){
            HSSFWorkbook hssfWorkbook = null;
        }*/
        XSSFWorkbook xssfWorkbook = null;
        try {
            xssfWorkbook = new XSSFWorkbook(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取表格的第一页
        Sheet xssfSheet = xssfWorkbook.getSheetAt(0);
        //获取该页有多少数据
        int rowNum= xssfSheet.getLastRowNum();
        System.err.println(rowNum);
        for(int i=1; i<=rowNum; i++){
            //获取当前行
            Row xssfRow = xssfSheet.getRow(i);
            //以此获得该行的所有单元格
            String trueName = xssfRow.getCell(1).toString();
            String sex = xssfRow.getCell(2).toString();
            String account = xssfRow.getCell(3).toString();
            String acatarUrl = xssfRow.getCell(4).toString();
            String email = xssfRow.getCell(5).toString();
            String password = xssfRow.getCell(6).toString();
            String remark = xssfRow.getCell(7).toString();
            String enabled = xssfRow.getCell(8).toString();
            String phone = xssfRow.getCell(9).toString();
            String idCard = xssfRow.getCell(10).toString();
            String birthDate = xssfRow.getCell(11).toString();
            String deptId = xssfRow.getCell(12).toString();
            String freestyle = xssfRow.getCell(13).toString();
            String registerDate = xssfRow.getCell(14).toString();
            String registerIp = xssfRow.getCell(15).toString();
            String status = xssfRow.getCell(16).toString();
            String grade = xssfRow.getCell(17).toString();
            String wages = xssfRow.getCell(18).toString();


            //将数据写入实体类
            jsUser.setWages(DateUtils.StrisInt(wages));
            jsUser.setGrade(DateUtils.StrisInt(grade));
            jsUser.setStatus(DateUtils.StrisInt(status));
            jsUser.setRegisterIp(registerIp);
            jsUser.setRegisterDate(DateUtils.transString(registerDate));
            jsUser.setFreestyle(freestyle);
            jsUser.setDeptId(DateUtils.StrisInt(deptId));
            jsUser.setBirthDate(DateUtils.transString(birthDate));
            jsUser.setTrueName(trueName);
            jsUser.setSex(sex);
            jsUser.setAccount(account);
            jsUser.setAcatarUrl(acatarUrl);
            jsUser.setEmail(email);
            jsUser.setPassword(password);
            jsUser.setRemark(remark);
            jsUser.setEnabled(DateUtils.StrisInt(enabled));
            jsUser.setPhone(phone);
            jsUser.setIdCard(idCard);


            System.err.println(jsUser);
            //将实体类插入数据库
            Boolean aBoolean = jurisdictionService.addExcel(jsUser);
            return aBoolean != false ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("服务端错误");
        }
        return JsonData.buildError("服务端错误");
    }


}
